package com.example.community.userHome;

import com.example.community.dao.ComplaintDao;
import com.example.community.model.Complaint;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;

@WebServlet(urlPatterns = "/addComplaintServlet")
public class AddComplaintServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/WEB-INF/userHome/AddComplaint.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Integer currentUserUsNo = (Integer) req.getSession().getAttribute("currentUserUsNo");
            String desc = req.getParameter("desc");

            Calendar calendar = Calendar.getInstance();

            Complaint complaint = new Complaint();
            complaint.setCpContent(desc);
            complaint.setUsNo(currentUserUsNo);
            complaint.setCpDt(calendar.getTime());
            complaint.setCpState("未处理");

            ComplaintDao complaintDao = new ComplaintDao();
            Integer insert = complaintDao.insert(complaint);
            if (insert == 1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }catch (Exception e){
            resp.getWriter().write("false");
        }
    }
}
